<?php

/**
 * Description of Customer
 *
 * @author Scott McKay
 */
class Application_Model_Customer extends Zend_Db_Table {

    protected $_name = 'Customer';

    /**
     * Function returns the name of a specified customer
     * 
     * @params The ID of the Customer
     */
    public function getName($id) {
        $sql = "SELECT Login FROM Customer WHERE Customer_ID = ?";
        $params = array($id);
        $rep = $this->getAdapter()->query($sql, $params)->fetchAll();
        return $rep[0]['Login'];
    }


    /**
     * Function returns a boolean, true if the login exists in the db.
     * 
     * @params The Name of a Customer
     */
    public function isExist($login) {
        $sql = "SELECT Login FROM Customer WHERE Login = ?";
        $params = array($login);
        $request = $this->getAdapter()->query($sql, $params)->fetchAll();
        if (count($request) >= 1)
            return true;
        return false;
    }

    public function addUser($login, $password) {
        /* $sql = "INSERT INTO Customer (Customer_ID, Login , Password, Picture) VALUES (NULL, 'babar', 'barbar', NULL) ";
          $params[0] = $login;
          $params[1] = md5($password);
          $request = $this->getAdapter()->getConnection()->query($sql);
          print_r($request); */

        $db = $this->getAdapter();
        $query = "
                INSERT INTO Customer (Customer_ID, Login , Password, Picture)
                VALUES(NULL, :Login, :Password, NULL);
        ";

        $params = array(
            ":Login" => $login,
            ":Password" => $password
        );

        $db->query($query, $params);
    }
    
    /**
     * Function to update user crredentials\
     * 
     * @param id The Customer_ID
     * @param login The customers new login name
     * @param password The customers new password
     */
    public function updateCustomer($id, $login, $password, $picture) {
		$sql = "UPDATE Customer c Set c.Login = :login, c.Password = :pass, c.Picture = :pic WHERE c.Customer_ID = :id";
		$params = array(":login" => $login, ":pass" => $password, ":pic" => $picture, ":id" => $id);
		$this->getAdapter()->query($sql, $params);
	}
	
	/**
	 * Function to delete a customer from the database
	 * 
	 * @param id The customer to remove
	 */
	 public function deleteCustomer($id) {
		 $sql = "DELETE FROM Customer WHERE Customer_ID = ?";
		 $params = array($id);
		 $this->getAdapter()->query($sql, $params);
	 }

    public function login($login, $password) {


        $sql = "SELECT Customer_ID FROM Customer WHERE Login = :Login AND Password = :Password;";
        $params = array(
            ":Login" => $login,
            ":Password" => $password
        );

        $request = $this->getAdapter()->query($sql, $params)->fetchAll();

        if (count($request) >= 1) {
            return $request[0]["Customer_ID"];
        }
        return -1;
    }

}
